var app = getApp()
const date = new Date();
const years = [];
const months = [];
//获取年
for (let i = 2018; i <= date.getFullYear() + 5; i++) {
years.push("" + i);
}
//获取月份
for (let i = 1; i <= 12; i++) {
if (i < 10) {
 i = "0" + i;
}
months.push("" + i);
}
Page({
	data: {
		isShowtip:true,
		showLoading: true,
		noMoretip: false,
		page: 0,
		fxstatus:1,
		areaName:'',
		ordertotal:'0.00',
		areatotal:'0.00',
		time: '',
		multiArray: [years, months],
		multiIndex: [0, 9],
		choose_year: '',
		ygIndex:0,
		ygArray:[],
		ygArray2:[],
		yuangong:'',
		yg_m_id:0
	},
	onLoad: function (options) {
		var that = this;		
		that.requestShareincomelist();
	},
	onShow:function(){
	
	},
	//获取时间日期
	bindMultiPickerChange: function(e) {	    
	    this.setData({
	      multiIndex: e.detail.value
	    })
	    const index = this.data.multiIndex;
	    const year = this.data.multiArray[0][index[0]];
	    const month = this.data.multiArray[1][index[1]];
	    this.setData({
	      time: year + '-' + month
	    })
		this.requestShareincomelist()
	},
	bindYGPickerChange:function(e){
		var that=this;
		var ygindex =e.detail.value;
		var ygnickname =that.data.ygArray2[ygindex];		
		var yg_m_id =0;
		that.data.ygArray.forEach(function(item,index){
			if(item.m_nickname==ygnickname){
				yg_m_id =item.ma_m_id;
			}
		})
		that.setData({
			yg_m_id:yg_m_id
		})		
		console.log(that.data.yg_m_id);
		
		that.requestShareincomelist();
		
	},
	bindMultiPickerColumnChange: function(e) {
	      //获取年份
	      if (e.detail.column == 0) {
	        let choose_year = this.data.multiArray[e.detail.column][e.detail.value];
	        console.log(choose_year);
	        this.setData({
	          choose_year
	        })
	      }
	      
	      var data = {
	        multiArray: this.data.multiArray,
	        multiIndex: this.data.multiIndex
	      };
	      data.multiIndex[e.detail.column] = e.detail.value;
	      this.setData(data);
	},
	closeShowtip:function(){//关闭提示
		this.setData({
			isShowtip:false
		})
	},
	requestShareincomelist: function () {
		var that = this;
		var data = {};
		var page = that.data.page;
        data.map = 'applet_parentarea_order_list';
		data.page = page;
		data.time = that.data.time;
		data.yg_m_id =that.data.yg_m_id;
		wx.request({
			url: app.globalData.requestUrl,
			data: data,
			success: function (res) {
				console.log(page);
				console.log(res.data);
				if (res.data.ec == 200) {
					var allArr = [];
					var initArr = that.data.shareincomeList ? that.data.shareincomeList : [];
					var curArr = res.data.data;
					var lastPageLength = curArr.length;
					if (page > 0) {
						allArr = initArr.concat(curArr);
					} else {
						allArr = res.data.data;
					}
					
					if(res.data.mlist.length>0){
						var ygArray2 =[];
						res.data.mlist.forEach(function(ite){
							ygArray2.push(ite.m_nickname);
						})
						console.log(ygArray2);
					}
					
					that.setData({
						shareincomeList: allArr,
						ordertotal :res.data.ordertotal,
						areatotal  :res.data.areatotal,
						ygArray   :res.data.mlist,
						ygArray2  :ygArray2
					})
					
					console.log(that.data.ygArray);
					
					if (lastPageLength < 10) {
						that.setData({
							noMoretip: true,
							showLoading: false
						});
					}
					console.log(that.data.shareincomeList);
				} else {
					if (page <= 0) {
						that.setData({
							shareincomeList: [],
							noMoretip: false,
							showLoading: false
						})
					} else {
						that.setData({
							noMoretip: true,
							showLoading: false
						});
					}
				}
			},
			complete: function () {
				wx.hideToast();
				wx.stopPullDownRefresh();
			}
		});
	},
	onPullDownRefresh: function () {
		var that = this;
		that.setData({
			page: 0,
			noMoretip: false,
			showLoading: true
		});
		that.requestShareincomelist();
		console.log("下拉刷新");
	},
	onReachBottom: function () {
		var that = this;
		console.log("到达页面底部")
		var isMore = that.data.noMoretip;
		var page = that.data.page;
		page++;
		that.setData({
			page: page
		});
		if (isMore) {
			console.log("已完成或正在加载");
		} else {
			that.requestShareincomelist();
		}
	},
})